<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form
                class="mb-[-16px]"
                :model="queryParams"
                inline
            >
                <el-form-item label="标题" prop="title">
                    <el-input class="w-56" v-model="queryParams.title" clearable placeholder="请输入标题" />
                </el-form-item>
                <el-form-item label="状态：" prop="status">
                  <el-select v-model="queryParams.status" placeholder="请选择状态">
                    <el-option label="禁用" value="0">禁用</el-option>
                    <el-option label="启用" value="1">启用</el-option>
                  </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-button v-perms="['advInfo.adv_info/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button>
            <el-button
                v-perms="['advInfo.adv_info/delete']"
                :disabled="!selectData.length"
                @click="handleDelete(selectData)"
            >
                删除
            </el-button>
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="ID" prop="id" />
                    <el-table-column label="广告位置" prop="adv_id" />
                    <el-table-column label="排序" prop="sort" />
                    <el-table-column label="标题" prop="title" />
                    <el-table-column  label="图片">
                      <template #default="{ row }">
                        <el-image v-for="(item, index) in [scope.row.img]" :key='index' style="width: 50px; height: 50px" :src="item" :preview-src-list="[item]"></el-image>
                      </template>
                    </el-table-column>
                    <el-table-column label="状态" prop="status">
                      <template #default="{ row }">
                        <span v-if="row.status === 1">启用</span>
                        <span v-else>禁用</span>
                      </template>
                    </el-table-column>
                    <el-table-column label="类型" prop="type">
                      <template #default="{ row }">
                        <span v-if="row.status === 0">固定</span>
                        <span v-else-if="row.status === 1">指定列表</span>
                        <span v-else-if="row.status === 2">商品详情</span>
                        <span v-else-if="row.status === 3">外部url</span>
                        <span v-else></span>
                      </template>
                    </el-table-column>
                    <el-table-column label="关联字段id" prop="param_id" />
                    <el-table-column label="点击跳转的url地址" prop="url" />
                    <el-table-column label="添加时间" prop="created_at" />
                    <el-table-column label="修改时间" prop="updated_at" />
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                             <el-button
                                v-perms="['advInfo.adv_info/edit']"
                                type="primary"
                                link
                                @click="handleEdit(row)"
                            >
                                编辑
                            </el-button>
                            <el-button
                                v-perms="['advInfo.adv_info/delete']"
                                type="danger"
                                link
                                @click="handleDelete(row.id)"
                            >
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup>
import { usePaging } from '@/hooks/usePaging'
import { dictDataLists } from '@/api/setting/dict'
import { apiAdvInfoLists, apiAdvInfoDelete } from '@/api/adv_info'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)


// 查询条件
const queryParams = reactive({
    adv_id: '',
    sort: '',
    title: '',
    status: '',
    created_at: '',
    updated_at: ''
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 字典数据
const dictData = reactive<Record<string, any[]>>({

})

// 获取字典数据
const getDictData = () => {

}

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiAdvInfoLists,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiAdvInfoDelete({ id })
    getLists()
}

getLists()
getDictData()
</script>

